<template>
  <div class="order-container">
    <div class="order-header">
      <div class="order-header-content">
        <p>
          <i
            class="el-icon-s-order"
            style="font-size: 30px; color: #ff6700"
          ></i>
          我的订单
        </p>
      </div>
    </div>

    <div v-if="orderList.length > 0">
      <div v-for="order in orderList" :key="order.id" class="order-content">
        <div class="order-content-header">
          订单号：{{ order.orderCode }}
          <el-button v-show="order.status === 0" @click="deleteOrder(order.id)" type="text" size="mini">
            删除订单
          </el-button>
          <el-button
            type="text"
            size="mini"
            v-show="order.status === 0"
            @click="
              $router.push({ path: '/order/confirm', query: { id: order.id } })
            "
            >去结算</el-button
          >
        </div>
        <div
          class="order-content-item"
          v-for="orderItem in order.orderItemList"
          :key="orderItem.id"
        >
          <img
            :src="getImageUrl(orderItem.item.imageUrl)"
            style="width: 80px; height: 80px"
            align="middle"
          />
          <div style="width: 30%">{{ orderItem.item.name }}</div>
          <div style="width: 20%">
            ￥{{ orderItem.salePrice }} × {{ orderItem.itemCount }}
          </div>
          <div style="width: 20%; color: #ff6700">￥{{ orderItem.totalPrice }}</div>
          <div style="width: 10%">{{ order.status }}</div>
        </div>
      </div>
      <div style="text-align: center">
        <el-pagination @current-change="handleCurrentChange" :page-size="size" :total="total" background layout="prev, pager, next"></el-pagination>
      </div>
    </div>
    <MallEmpty title="您的订单还是空的！" v-else></MallEmpty>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import MallEmpty from '@/components/MallEmpty'
import { mixin } from '@/mixins'
import { SPACE, deleteByIds, selectPageByForeignId } from '@/api/baseApi'
export default {
  mixins: [mixin],
  components: {
    MallEmpty
  },
  data() {
    return {
      size: 5,
      orderList: []
    }
  },
  activated() {
    this.findRow()
  },
  watch: {
  },
  methods: {
    ...mapGetters(['getUser']),
    deleteOrder(id) {
      deleteByIds(SPACE.order, [id]).then(res => {
        if (res.state === 200) {
          this.showSuccess()
          this.orderList = this.orderList.filter(order => order.id !== id)
        } else {
          this.showError()
        }
      })
    },
    async findRow() {
      const userId = this.getUser().id
      selectPageByForeignId(SPACE.order, this.current, this.size, userId).then(res => {
        this.orderList = res.state === 200 ? res.data.records : []
        this.total = res.data.total
        console.log(JSON.stringify(this.orderList))
      })
    }
  }
}
</script>
<style scoped>
.order-container {
  background-color: #f5f5f5;
  padding-bottom: 20px;
}
.order-container .order-header {
  height: 64px;
  border-bottom: 2px solid #ff6700;
  background-color: #fff;
  margin-bottom: 20px;
}
.order-container .order-header .order-header-content {
  width: 1225px;
  margin: 0 auto;
}
.order-container .order-header p {
  font-size: 28px;
  line-height: 58px;
  float: left;
  font-weight: normal;
  color: #424242;
}

.order-content {
  width: 1225px;
  margin: 10px auto;
  border: 1px solid #ddd;
}

.order-content:hover {
  border: 1px solid rgb(106, 164, 230);
}

.order-content .order-content-header {
  background-color: #e3eefe;
  padding: 8px 10px;
}
.order-content .order-content-header .el-button {
  float: right;
  margin: 0 10px;
}
.order-content .order-content-item {
  background-color: #fff;
  height: 80px;
  padding: 20px;
  border-bottom: 1px solid #e0e0e0;
}
.order-content .order-content-item div {
  display: inline-block;
  padding: 0 10px;
}
</style>
